#include <iostream>
#include <queue>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
using namespace std;
const int maxn = 1e6+7;
int node[maxn][26], cnt=0, num[maxn];
int value[maxn];
vector<int> Map[maxn];
void Init() {
    //for (int i=0; i<cnt+5; ++i) Map[i].clear();
    memset(node[0], 0, sizeof(node[0]));
    //memset(num, 0, sizeof(num));//4*(cnt+5));
    //cout<<"size: "<<sizeof(num)<<endl;
    //memset(value, 0x3f, sizeof(value));//4*(cnt+5));
    cnt = 0;
    value[0] = 0;
}
void Insert(char* s) {
    int now=0;
    int len=strlen(s);
    for (int i=0; i<len; ++i) {
        int t = s[i] - 'a';
        int &k = node[now][t];
        if (!k) {
            ++cnt;
            num[cnt] = 0;
            value[cnt] = value[now] + 1;
            Map[cnt].clear();
            memset(node[cnt], 0, sizeof(node[cnt]));
            node[now][t] = cnt;
            Map[now].push_back(cnt);
            Map[cnt].push_back(now);
        }
        ++num[k];
        now = k;
    }
    int last = now;
    //cout<<"last: "<<last<<endl;
    now = 0;
    len -= 1;
    for (int i=0; i<len; ++i) {
        int t = s[i] - 'a';
        int k = node[now][t];
        if (num[k] == 1) {
            Map[k].push_back(last);
        }
        now = k;
    }
}
void BFS() {
    queue<int> q;
    q.push(0);
    value[0] = 0;
    while (!q.empty()) {
        int pre = q.front(); q.pop();
        int len = Map[pre].size();
        for (int i=0; i<len; ++i) {
            int nex = Map[pre][i];
            if (value[pre] + 1 <= value[nex]) {
                value[nex] = value[pre] + 1;
                q.push(nex);
            }
        }
    }
}
int solve(char* s) {
    int res=0;
    int len = strlen(s);
    int now = 0;
    int i = 0;
    for (; i<len; ++i) {
        int t = s[i] - 'a';
        int k = node[now][t];
        if (!k) break;
        res = min(res+1, value[k]);
        //cout<<s[i]<<" : "<<value[k]<<" "<<res<<endl;
        now = k;
    }
    res += (len-i);
    return res;
}
char ss[maxn];
int main(int argc, char** argv) {
    #ifdef hai
    freopen("in.txt", "r", stdin);
    #endif
    int n,m;
    while (cin>>n>>m) {
        Init();
        for (int i=0; i<n; ++i) {
            scanf("%s", ss);
            Insert(ss);
        }
        BFS();
        for (int i=0; i<m; ++i) {
            scanf("%s", ss);
            cout<<solve(ss)<<endl;
        }
        cout<<endl;
    }
    return 0;
}
